package edu.ccut.saturn.studio.saturndata.model;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import edu.ccut.saturn.studio.core.saturnproperty.StringUtils;

public abstract class JavaTypeUtils {

	private static Map<String, Map<String, String>> databaseTypeMap = new HashMap<String, Map<String, String>>();
	
	private static Map<String, String> oracle = new HashMap<String, String>();
	
	private static Map<String, String> mysql = new HashMap<String, String>();
	
	private static Map<String, String> db2 = new HashMap<String, String>();
	
	private static Map<String, String> sqlserver = new HashMap<String, String>();
	
	private static final String ORACLE_PROPERTIES = "oracle.properties";
	
	private static final String MYSQl_PROPERTIES = "mysql.properties";
	
	private static final String DB2_PROPERTIES = "db2.properties";
	
	private static final String SQLSERVER_PROPERTIES = "sqlserver.properties";
	
	static {
		init();
	}
	
	private static void init() {
		
		
		String[] type = {ORACLE_PROPERTIES, MYSQl_PROPERTIES, DB2_PROPERTIES, SQLSERVER_PROPERTIES};
		List<Map<String,String>> typelist = new ArrayList<Map<String,String>>();
		typelist.add(oracle);
		typelist.add(mysql);
		typelist.add(db2);
		typelist.add(sqlserver);
		
		try {
			for(int i = 0; i <4; i++) {
				Properties properties = new Properties();
				String[] stype = {"oracle", "mysql", "db2", "sqlserver"};
				InputStream is = JavaTypeUtils.class.getResourceAsStream(type[i]);
				properties.load(is);
				
				Enumeration<Object> keys = properties.keys();
				while(keys.hasMoreElements()){
					String key =(String) keys.nextElement();
					String value = (String)properties.getProperty(key);
					typelist.get(i).put(key, value);
			    }
				
				databaseTypeMap.put(stype[i], typelist.get(i));
			}	
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String[] getSelectedJavaType() {
		String[] javatype = { "java.lang.Object", "java.lang.String", //$NON-NLS-1$ //$NON-NLS-2$
				"java.lang.Character", "java.lang.Short", "java.lang.Byte", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				"java.lang.Integer", "java.lang.Long", "java.lang.Float", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				"java.lang.Double", "java.lang.Boolean", //$NON-NLS-1$ //$NON-NLS-2$
				"java.math.BigDecimal", "java.sql.Date","java.sql.ResultSet", "byte[]" };

		return javatype;
	}

	public static String getColumnJavaType(String database, String type) {
		if (StringUtils.isNotEmpty(database) && StringUtils.isNotEmpty(type)) {
			Map<String, String> databaseMap = databaseTypeMap.get(database.toLowerCase());
			if (databaseMap != null) {
				String value = databaseMap.get(type.toLowerCase());
				
				if (value != null)
					return value;
			}
		}
		
		return String.class.getName();
	}
}
